package com.power.sifa.approve.executor.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.plf.common.domain.YesNo;
import com.power.sifa.approve.model.Apl_admre_audit;
import com.power.sifa.approve.model.Apl_admreview;
import com.power.sifa.approve.model.Apl_thirdper;
import com.power.sifa.approve.model.enumeration.Tp_proctype;
import com.power.sifa.approve.model.enumeration.Tp_proctypeEnum;
import com.power.sifa.approve.service.*;
import com.power.sifa.util.executor.AbstractPostExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xiongzhiwen
 * @version 1.0
 * @description: 第三人新增服务
 * @date 2024/4/17 15:18
 */

@Component
@Slf4j
class HearAuditThirder extends AbstractPostExecutor {

    // 用户id
    private String userid;
    // 环节分类标识
    private String category;
    // 用户名
    private String username;
    // 单位id
    private String sectionid;
    // 单位名称
    private String sectionname;
    // 部门id
    private String deptid;
    // 部门名称
    private String deptname;
    // 处理意见
    private String remark;
    // 行政区划
    private String adid;
    // 环节名称
    private String taskname;

    @Resource
    private Apl_admreviewService apl_admreviewService;
    @Resource
    private Apl_mainService apl_mainService;
    @Resource
    private BusinessApproveService businessApproveService;

    @Resource
    private Apl_thirdperService apl_thirdperService;

    @Resource
    private Apl_admre_auditService apl_admre_auditService;


    @Override
    public void init(Map<String, Object> initdata) {
        this.userid = (String) initdata.get("userid");
        this.category = (String) initdata.get("category");
        this.username = (String) initdata.get("username");
        this.sectionid = (String) initdata.get("sectionid");
        this.sectionname = (String) initdata.get("sectionname");
        this.deptid = (String) initdata.get("deptid");
        this.deptname = (String) initdata.get("deptname");
        this.remark = (String) initdata.get("remark");
        this.adid = (String) initdata.get("adid");
        this.taskname = (String) initdata.get("taskname");
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> execute() throws Exception {
        // 1. 生成和保存三级审批文书
        Apl_admre_audit auditObj = this.businessApproveService.createThreeLevelApprove(super.applyid, this.userid, this.username, Tp_proctype.HEAR_THIRDER);

        // 2. 删除一个子状态
        Apl_admreview admreObj = this.apl_admreviewService.getById(auditObj.getApplyid());
        String sonStatus = this.businessApproveService.delSonStatus(admreObj.getSonstatus(), Tp_proctypeEnum.HEAR_THIRDER.getStatus());
        this.apl_admreviewService.updatesSonStatus(admreObj.getId(), sonStatus);

        // 3.更新第三人的状态
        this.apl_thirdperService.updateUnSaveThirder(auditObj.getApplyid());
        // 查询全部的第三人
        List<Apl_thirdper> thirdList = this.apl_thirdperService.getThirdListByApplyid(auditObj.getApplyid());
        if (CollectionUtil.isNotEmpty(thirdList)) {
            String thirdName = String.join("，", thirdList.stream().filter(el -> ObjectUtil.isNotNull(el.getName())).map(Apl_thirdper::getName).collect(Collectors.toList()));
            // 更新主表的记录
            admreObj.setIsthird(YesNo.YES);
            admreObj.setThirdname(thirdName);
            admreObj.setSonstatus(sonStatus);
            this.apl_admreviewService.saveOrUpdate(admreObj);
            // 更新全部的子表记录
            List<Apl_admre_audit> auditList = this.apl_admre_auditService.listAdmreAuditByApplyid(auditObj.getApplyid());
            if (CollectionUtil.isNotEmpty(auditList)) {
                auditList.stream().forEach(el -> {
                    el.setThirdper(thirdName);
                    this.apl_admre_auditService.saveOrUpdate(el);
                });
            }
        }
        // 刷新文书日期
        this.businessApproveService.processEndedUpdatePreviewDoc(auditObj.getApplyid(), Tp_proctype.HEAR_THIRDER, null);
        // 文书盖章
        this.businessApproveService.templateStamp(auditObj.getApplyid(), Tp_proctype.HEAR_THIRDER);
        return null;
    }
}
